//
//  ShoppingTableViewCell.swift
//  CJXSwiftStoryboard
//
//  Created by CJX on 2016/10/22.
//  Copyright © 2016年 caijianxi. All rights reserved.
//

import UIKit

protocol ShoppingTableViewCellDelegate {
    func goToCommodityVC(_ productID:String,imgStr:String)
    func letShoppingVCDeleteTheCell(_ tag:Int)
    func letShoppingVCSubTheNumber(_ tag:Int)
    func letShoppingVCAddTheNumber(_ tag:Int)
}

class ShoppingTableViewCell: UITableViewCell {

    var delegate : ShoppingTableViewCellDelegate?
    
    @IBOutlet weak var shoesImgView: UIImageView!
    @IBOutlet weak var shoesNameLabel: UILabel!
    @IBOutlet weak var shoesSizeLabel: UILabel!
    @IBOutlet weak var shoesPriceLabel: UILabel!
    @IBOutlet weak var shoesNumberLabel: UILabel!
    
    var productID = ""
    var productImageStr = ""
    
    var deleteNumber = 0
    var subNumebr = 0
    var addNumber = 0
    
    
    
    
    var index : Int?{
        didSet{
            let deleteBtn = self.viewWithTag(6)as?UIButton
            deleteBtn?.backgroundColor = UIColor(red: 224/255.0, green: 224/255.0, blue: 224/255.0, alpha: 1)
            deleteBtn?.setTitleColor(UIColor.black, for: UIControlState())
            deleteNumber = index! + 100
            
            shoesNumberLabel.backgroundColor = UIColor(red: 240/255.0, green: 240/255.0, blue: 240/255.0, alpha: 1)
            
            let subBtn = self.viewWithTag(7)as?UIButton
            subBtn?.backgroundColor = UIColor(red: 240/255.0, green: 240/255.0, blue: 240/255.0, alpha: 1)
            subNumebr = index! + 1000
            
            let addBtn = self.viewWithTag(9)as?UIButton
            addBtn?.backgroundColor = UIColor(red: 240/255.0, green: 240/255.0, blue: 240/255.0, alpha: 1)
            addNumber = index! + 10000
        }
    }
    
    
    
    var model : ShoppingCartModel?{
        didSet{
            
            shoesImgView.sd_setImage(with: URL(string: (model?.products_image)!))
            shoesNameLabel.text = model?.products_name
            shoesSizeLabel.text = model?.products_options_values_name
            shoesPriceLabel.text = model?.format_price
            shoesNumberLabel.text = model?.quantity
            
            productID = (model?.products_id)!
            productImageStr = (model?.products_image)!
        }
    }
    
    
    @IBAction func viewBtnClick(_ sender: AnyObject) {
        print("index>>>>>>" + String(sender.tag))

        self.delegate?.goToCommodityVC(productID, imgStr: productImageStr)
    }
    
    @IBAction func delleteBtnClick(_ sender: AnyObject) {
        
        self.delegate?.letShoppingVCDeleteTheCell(deleteNumber)
    }
    
    
    @IBAction func subBtnClick(_ sender: AnyObject) {
        
        self.delegate?.letShoppingVCSubTheNumber(subNumebr)

    }
    
    @IBAction func addBtnClick(_ sender: AnyObject) {
        self.delegate?.letShoppingVCAddTheNumber(addNumber)

    }

    override func awakeFromNib() {
        super.awakeFromNib()
        shoesImgView.contentMode = .scaleAspectFit
        shoesImgView.clipsToBounds = true
        
        let viewBtn = self.viewWithTag(5)as?UIButton
        viewBtn?.backgroundColor = UIColor(red: 217/255.0, green: 217/255.0, blue: 217/255.0, alpha: 1)
        viewBtn?.setTitleColor(UIColor.black, for: UIControlState())
        
//        let deleteBtn = self.viewWithTag(6)as?UIButton
//        deleteBtn?.backgroundColor = UIColor(red: 224/255.0, green: 224/255.0, blue: 224/255.0, alpha: 1)
//        deleteBtn?.setTitleColor(UIColor.blackColor(), forState: .Normal)
//        
//        shoesNumberLabel.backgroundColor = UIColor(red: 240/255.0, green: 240/255.0, blue: 240/255.0, alpha: 1)
//        
//        let subBtn = self.viewWithTag(7)as?UIButton
//        subBtn?.backgroundColor = UIColor(red: 240/255.0, green: 240/255.0, blue: 240/255.0, alpha: 1)
//        
//        let addBtn = self.viewWithTag(9)as?UIButton
//        addBtn?.backgroundColor = UIColor(red: 240/255.0, green: 240/255.0, blue: 240/255.0, alpha: 1)

    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
